Please do not discuss the contents
of this test with anyone before the evening of Friday, 10 October.
You have two hours to complete
this test. Please put your login on each sheet, as indicated, in
case pages get separated. Answer all questions in the space provided
on the exam paper. Show all work (but be sure to indicate your answers
clearly.) The exam is worth a total of 35+ points (out of the total
of 200), distributed as indicated on the individual questions.
You may use any notes, books, or computers
you please--anything inanimate. We suggest that you read all questions
before trying to answer any of them and work first on those about which
you feel most confident.
You should have 5 problems on 8 pages.
1. __________/9
2. __________/9
3. __________/
4. __________/9
5. __________/8
TOT. __________/35
1. [9 points] A certain language has the following terminal symbols:
@ * # i /
A shift-reduce parser for this language processes the string
i * i # @ i /
and performs the following actions:
2. Reduce 0 symbols (on the stack), producing (the non-terminal symbol) N. Then reduce 2 symbols, producing T. Then reduce 1 symbol, producing F.
3. Shift *. Then shift i. Then shift #.
4. Reduce 1 symbol, producing N. Then reduce 2 symbols, producing T.
5. Shift @. Then shift i.
6. Reduce 0 symbols, producing N. Then reduce 2 symbols, producing T. Then reduce 1 symbol, producing F. Then reduce 5 symbols, producing F.
7. Shift /.
8. Reduce 2 symbols, producing E and accepting the input.
(Continued on next page)
2. [9 points] Consider the following NFA:
b. Write a regular expression that describes the same
language. You may use Lex notation.
c. Write a BNF grammar that describes the same language.
4. [9 points] Consider the following grammar:
next() returns the syntactic category of
the next (as yet unprocessed) symbol of the input:
one of the
values
ERROR() reports an error.
Write your program on the next page
without using global variables. All assignments should be to local
variables only. Do not introduce any new types.
Here is the parser skeleton. Remember: no global
variables; assign only to local variables; do not introduce new types.
void start() { printf ("Maximum operators in any expression = %d\n", _________); } _____ prog () { } _____ stmt () { } _____ assign () { } _____ expr () { }
5. [8 points] In the following, do not worry about syntax trees or semantic actions; just consider the language being recognized. Symbols in single quotes or in all-upper-case are terminal symbols. It should not be necessary to build LALR(1) machines to answer any of these questions!
b. In the language Alphard, x.y could be written y(x). Assuming we want a LALR(1) parser, why do we get a reduce/reduce conflict on the ')' token in the following grammar? Give an example of an input that runs into the error. (Warning: is it ambiguous?)
Identify the bug in the grammar. Give an example of an input that runs into the error (that is, that requires making a shift/reduce decision for a state and symbol that have a conflict). Propose a simple change to the grammar (and the language it describes) to fix the problem (that is, make an informed guess as to what was really intended).
d. The following expression grammar makes addition and multiplication left-associative and exponentiation (**) right associative. What causes the shift-reduce conflicts on the symbols +, *, and **? Give an example of an input that runs into one of the errors. (Be careful: TYPE and ID are distinct tokens.)